package com.chhuang.huaao.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.core.bean.QueryBean;
import com.chhuang.utils.Assert;
import com.chhuang.huaao.bean.HaMachineQueryBean;
import com.chhuang.huaao.po.HaMachine;
import com.chhuang.huaao.po.HaMachineExample;
import com.chhuang.huaao.mapper.HaMachineMapper;
import com.chhuang.huaao.service.IHaMachineService;

/**
* HaMachine ServiceImpl
* @author ChHuang
* @date Sun Apr 16 08:43:49 CST 2017
*/
@Service
public class HaMachineServiceImpl extends BaseServiceImpl<HaMachineMapper, HaMachine> implements IHaMachineService {
	@Autowired
	private HaMachineMapper haMachineMapper;

	@Override
	public QueryBean<HaMachine> seniorQuery(QueryBean<HaMachine> queryBean) throws Exception {
		HaMachineQueryBean poQueryBean = (HaMachineQueryBean)queryBean;
		HaMachineExample example = new HaMachineExample();
		
		if(poQueryBean.isAnd()){//全部是与条件
			HaMachineExample.Criteria criteria = example.createCriteria();
			if(Assert.isValidString(poQueryBean.getMachineId())){
				criteria.andMachineIdEqualTo(poQueryBean.getMachineId());
			}
			if(Assert.isValidString(poQueryBean.getMachineNum())){
				criteria.andMachineNumLike("%"+poQueryBean.getMachineNum()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineAname())){
				criteria.andMachineAnameLike("%"+poQueryBean.getMachineAname()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineCname())){
				criteria.andMachineCnameLike("%"+poQueryBean.getMachineCname()+"%");
			}
			if(poQueryBean.getMachineOnlineStatus() != null){
				criteria.andMachineOnlineStatusEqualTo(poQueryBean.getMachineOnlineStatus());
			}
			if(poQueryBean.getMachineRunStatus() != null){
				criteria.andMachineRunStatusEqualTo(poQueryBean.getMachineRunStatus());
			}
			if(poQueryBean.getMachineRuningStatus() != null){
				criteria.andMachineRuningStatusEqualTo(poQueryBean.getMachineRuningStatus());
			}
			if(poQueryBean.getMachineBinStatus() != null){
				criteria.andMachineBinStatusEqualTo(poQueryBean.getMachineBinStatus());
			}
			if(poQueryBean.getMachineStoveStatus() != null){
				criteria.andMachineStoveStatusEqualTo(poQueryBean.getMachineStoveStatus());
			}
			if(poQueryBean.getMachineBlowerStatus() != null){
				criteria.andMachineBlowerStatusEqualTo(poQueryBean.getMachineBlowerStatus());
			}
			if(poQueryBean.getMachineWindStatus() != null){
				criteria.andMachineWindStatusEqualTo(poQueryBean.getMachineWindStatus());
			}
			if(poQueryBean.getMachinePumpStatus() != null){
				criteria.andMachinePumpStatusEqualTo(poQueryBean.getMachinePumpStatus());
			}
			if(poQueryBean.getMachineFireStatus() != null){
				criteria.andMachineFireStatusEqualTo(poQueryBean.getMachineFireStatus());
			}
			if(poQueryBean.getMachineWaterTemp() != null){
				criteria.andMachineWaterTempEqualTo(poQueryBean.getMachineWaterTemp());
			}
			if(poQueryBean.getMachineSmokeTemp() != null){
				criteria.andMachineSmokeTempEqualTo(poQueryBean.getMachineSmokeTemp());
			}
			if(poQueryBean.getMachineInletWaterTemp() != null){
				criteria.andMachineInletWaterTempEqualTo(poQueryBean.getMachineInletWaterTemp());
			}
			if(poQueryBean.getMachineHightTempWarn() != null){
				criteria.andMachineHightTempWarnEqualTo(poQueryBean.getMachineHightTempWarn());
			}
			if(poQueryBean.getMachineLessWaterWarn() != null){
				criteria.andMachineLessWaterWarnEqualTo(poQueryBean.getMachineLessWaterWarn());
			}
			if(poQueryBean.getMachineLessFuelWarn() != null){
				criteria.andMachineLessFuelWarnEqualTo(poQueryBean.getMachineLessFuelWarn());
			}
			if(poQueryBean.getMachineBinStall() != null){
				criteria.andMachineBinStallEqualTo(poQueryBean.getMachineBinStall());
			}
			if(poQueryBean.getMachineTurnOffFireTemp() != null){
				criteria.andMachineTurnOffFireTempEqualTo(poQueryBean.getMachineTurnOffFireTemp());
			}
			if(poQueryBean.getMachineTurnOnFireTemp() != null){
				criteria.andMachineTurnOnFireTempEqualTo(poQueryBean.getMachineTurnOnFireTemp());
			}
			if(poQueryBean.getMachineTurnSmokeTemp() != null){
				criteria.andMachineTurnSmokeTempEqualTo(poQueryBean.getMachineTurnSmokeTemp());
			}
			if(poQueryBean.getMachineTurnOnPumpTemp() != null){
				criteria.andMachineTurnOnPumpTempEqualTo(poQueryBean.getMachineTurnOnPumpTemp());
			}
			if(poQueryBean.getMachineTurnOffPumpTemp() != null){
				criteria.andMachineTurnOffPumpTempEqualTo(poQueryBean.getMachineTurnOffPumpTemp());
			}
			if(poQueryBean.getMachineTurnOnTimeout() != null){
				criteria.andMachineTurnOnTimeoutEqualTo(poQueryBean.getMachineTurnOnTimeout());
			}
			if(poQueryBean.getMachineTurnOnPreFeedTimeout() != null){
				criteria.andMachineTurnOnPreFeedTimeoutEqualTo(poQueryBean.getMachineTurnOnPreFeedTimeout());
			}
			if(poQueryBean.getMachineTurnOffBlowerTimeout() != null){
				criteria.andMachineTurnOffBlowerTimeoutEqualTo(poQueryBean.getMachineTurnOffBlowerTimeout());
			}
			if(poQueryBean.getMachineTurnOffWindTimeout() != null){
				criteria.andMachineTurnOffWindTimeoutEqualTo(poQueryBean.getMachineTurnOffWindTimeout());
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval1())){
				criteria.andMachineWorkTimeInterval1Like("%"+poQueryBean.getMachineWorkTimeInterval1()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval2())){
				criteria.andMachineWorkTimeInterval2Like("%"+poQueryBean.getMachineWorkTimeInterval2()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval3())){
				criteria.andMachineWorkTimeInterval3Like("%"+poQueryBean.getMachineWorkTimeInterval3()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval4())){
				criteria.andMachineWorkTimeInterval4Like("%"+poQueryBean.getMachineWorkTimeInterval4()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval5())){
				criteria.andMachineWorkTimeInterval5Like("%"+poQueryBean.getMachineWorkTimeInterval5()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval6())){
				criteria.andMachineWorkTimeInterval6Like("%"+poQueryBean.getMachineWorkTimeInterval6()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval7())){
				criteria.andMachineWorkTimeInterval7Like("%"+poQueryBean.getMachineWorkTimeInterval7()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval8())){
				criteria.andMachineWorkTimeInterval8Like("%"+poQueryBean.getMachineWorkTimeInterval8()+"%");
			}
			if(poQueryBean.getMachineAdminDeadline() != null){
				criteria.andMachineAdminDeadlineEqualTo(poQueryBean.getMachineAdminDeadline());
			}
			if(Assert.isValidString(poQueryBean.getMachineAddress())){
				criteria.andMachineAddressLike("%"+poQueryBean.getMachineAddress()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				criteria.andCustomerIdLike("%"+poQueryBean.getCustomerId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId1())){
				criteria.andCustomerId1Like("%"+poQueryBean.getCustomerId1()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId2())){
				criteria.andCustomerId2Like("%"+poQueryBean.getCustomerId2()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId3())){
				criteria.andCustomerId3Like("%"+poQueryBean.getCustomerId3()+"%");
			}
			if(poQueryBean.getCreateTime() != null){
				criteria.andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
			if(poQueryBean.getModifyTime() != null){
				criteria.andModifyTimeEqualTo(poQueryBean.getModifyTime());
			}
			if(poQueryBean.getHeartTime() != null){
				criteria.andHeartTimeEqualTo(poQueryBean.getHeartTime());
			}
		}else{//全部是或条件
			if(Assert.isValidString(poQueryBean.getMachineId())){
				example.or().andMachineIdEqualTo(poQueryBean.getMachineId());
			}
			if(Assert.isValidString(poQueryBean.getMachineNum())){
				example.or().andMachineNumLike("%"+poQueryBean.getMachineNum()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineAname())){
				example.or().andMachineAnameLike("%"+poQueryBean.getMachineAname()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineCname())){
				example.or().andMachineCnameLike("%"+poQueryBean.getMachineCname()+"%");
			}
			if(poQueryBean.getMachineOnlineStatus() != null){
				example.or().andMachineOnlineStatusEqualTo(poQueryBean.getMachineOnlineStatus());
			}
			if(poQueryBean.getMachineRunStatus() != null){
				example.or().andMachineRunStatusEqualTo(poQueryBean.getMachineRunStatus());
			}
			if(poQueryBean.getMachineRuningStatus() != null){
				example.or().andMachineRuningStatusEqualTo(poQueryBean.getMachineRuningStatus());
			}
			if(poQueryBean.getMachineBinStatus() != null){
				example.or().andMachineBinStatusEqualTo(poQueryBean.getMachineBinStatus());
			}
			if(poQueryBean.getMachineStoveStatus() != null){
				example.or().andMachineStoveStatusEqualTo(poQueryBean.getMachineStoveStatus());
			}
			if(poQueryBean.getMachineBlowerStatus() != null){
				example.or().andMachineBlowerStatusEqualTo(poQueryBean.getMachineBlowerStatus());
			}
			if(poQueryBean.getMachineWindStatus() != null){
				example.or().andMachineWindStatusEqualTo(poQueryBean.getMachineWindStatus());
			}
			if(poQueryBean.getMachinePumpStatus() != null){
				example.or().andMachinePumpStatusEqualTo(poQueryBean.getMachinePumpStatus());
			}
			if(poQueryBean.getMachineFireStatus() != null){
				example.or().andMachineFireStatusEqualTo(poQueryBean.getMachineFireStatus());
			}
			if(poQueryBean.getMachineWaterTemp() != null){
				example.or().andMachineWaterTempEqualTo(poQueryBean.getMachineWaterTemp());
			}
			if(poQueryBean.getMachineSmokeTemp() != null){
				example.or().andMachineSmokeTempEqualTo(poQueryBean.getMachineSmokeTemp());
			}
			if(poQueryBean.getMachineInletWaterTemp() != null){
				example.or().andMachineInletWaterTempEqualTo(poQueryBean.getMachineInletWaterTemp());
			}
			if(poQueryBean.getMachineHightTempWarn() != null){
				example.or().andMachineHightTempWarnEqualTo(poQueryBean.getMachineHightTempWarn());
			}
			if(poQueryBean.getMachineLessWaterWarn() != null){
				example.or().andMachineLessWaterWarnEqualTo(poQueryBean.getMachineLessWaterWarn());
			}
			if(poQueryBean.getMachineLessFuelWarn() != null){
				example.or().andMachineLessFuelWarnEqualTo(poQueryBean.getMachineLessFuelWarn());
			}
			if(poQueryBean.getMachineBinStall() != null){
				example.or().andMachineBinStallEqualTo(poQueryBean.getMachineBinStall());
			}
			if(poQueryBean.getMachineTurnOffFireTemp() != null){
				example.or().andMachineTurnOffFireTempEqualTo(poQueryBean.getMachineTurnOffFireTemp());
			}
			if(poQueryBean.getMachineTurnOnFireTemp() != null){
				example.or().andMachineTurnOnFireTempEqualTo(poQueryBean.getMachineTurnOnFireTemp());
			}
			if(poQueryBean.getMachineTurnSmokeTemp() != null){
				example.or().andMachineTurnSmokeTempEqualTo(poQueryBean.getMachineTurnSmokeTemp());
			}
			if(poQueryBean.getMachineTurnOnPumpTemp() != null){
				example.or().andMachineTurnOnPumpTempEqualTo(poQueryBean.getMachineTurnOnPumpTemp());
			}
			if(poQueryBean.getMachineTurnOffPumpTemp() != null){
				example.or().andMachineTurnOffPumpTempEqualTo(poQueryBean.getMachineTurnOffPumpTemp());
			}
			if(poQueryBean.getMachineTurnOnTimeout() != null){
				example.or().andMachineTurnOnTimeoutEqualTo(poQueryBean.getMachineTurnOnTimeout());
			}
			if(poQueryBean.getMachineTurnOnPreFeedTimeout() != null){
				example.or().andMachineTurnOnPreFeedTimeoutEqualTo(poQueryBean.getMachineTurnOnPreFeedTimeout());
			}
			if(poQueryBean.getMachineTurnOffBlowerTimeout() != null){
				example.or().andMachineTurnOffBlowerTimeoutEqualTo(poQueryBean.getMachineTurnOffBlowerTimeout());
			}
			if(poQueryBean.getMachineTurnOffWindTimeout() != null){
				example.or().andMachineTurnOffWindTimeoutEqualTo(poQueryBean.getMachineTurnOffWindTimeout());
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval1())){
				example.or().andMachineWorkTimeInterval1Like("%"+poQueryBean.getMachineWorkTimeInterval1()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval2())){
				example.or().andMachineWorkTimeInterval2Like("%"+poQueryBean.getMachineWorkTimeInterval2()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval3())){
				example.or().andMachineWorkTimeInterval3Like("%"+poQueryBean.getMachineWorkTimeInterval3()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval4())){
				example.or().andMachineWorkTimeInterval4Like("%"+poQueryBean.getMachineWorkTimeInterval4()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval5())){
				example.or().andMachineWorkTimeInterval5Like("%"+poQueryBean.getMachineWorkTimeInterval5()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval6())){
				example.or().andMachineWorkTimeInterval6Like("%"+poQueryBean.getMachineWorkTimeInterval6()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval7())){
				example.or().andMachineWorkTimeInterval7Like("%"+poQueryBean.getMachineWorkTimeInterval7()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineWorkTimeInterval8())){
				example.or().andMachineWorkTimeInterval8Like("%"+poQueryBean.getMachineWorkTimeInterval8()+"%");
			}
			if(poQueryBean.getMachineAdminDeadline() != null){
				example.or().andMachineAdminDeadlineEqualTo(poQueryBean.getMachineAdminDeadline());
			}
			if(Assert.isValidString(poQueryBean.getMachineAddress())){
				example.or().andMachineAddressLike("%"+poQueryBean.getMachineAddress()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				example.or().andCustomerIdLike("%"+poQueryBean.getCustomerId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId1())){
				example.or().andCustomerId1Like("%"+poQueryBean.getCustomerId1()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId2())){
				example.or().andCustomerId2Like("%"+poQueryBean.getCustomerId2()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId3())){
				example.or().andCustomerId3Like("%"+poQueryBean.getCustomerId3()+"%");
			}
			if(poQueryBean.getCreateTime() != null){
				example.or().andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
			if(poQueryBean.getModifyTime() != null){
				example.or().andModifyTimeEqualTo(poQueryBean.getModifyTime());
			}
			if(poQueryBean.getHeartTime() != null){
				example.or().andHeartTimeEqualTo(poQueryBean.getHeartTime());
			}
		}
		
		poQueryBean.setItemCount(haMachineMapper.countByExample(example));//总记录数
		if(poQueryBean.getItemCount()>0){//计算总页数
			poQueryBean.setPageCount(poQueryBean.getItemCount()/poQueryBean.getPageSize()+
					(int)(poQueryBean.getItemCount()%poQueryBean.getPageSize()>0?1:0));
			if(poQueryBean.getItemCount()>poQueryBean.getOffset()){
				if(Assert.isValidString(poQueryBean.getOrderBy())){//排序
					example.setOrderByClause(poQueryBean.getOrderBy()+" "+poQueryBean.getOrder());
				}
				//分页
				example.setOffset(poQueryBean.getOffset());
				example.setLimit(poQueryBean.getLimit());
				poQueryBean.setResult(haMachineMapper.selectByExample(example));
			}
		}
		
		return poQueryBean;
	}

	@Override
	public void updateNullMachineCustomerIds(String customerId) throws Exception{
		haMachineMapper.updateNullMachineCustomerId(customerId);
		haMachineMapper.updateNullMachineCustomerId1(customerId);
		haMachineMapper.updateNullMachineCustomerId2(customerId);
		haMachineMapper.updateNullMachineCustomerId3(customerId);
	}

	@Override
	public List<HaMachine> findMachinesByCustomerId(String customerId) throws Exception {
		HaMachineExample example = new HaMachineExample();
		example.or().andCustomerIdEqualTo(customerId);
		example.or().andCustomerId1EqualTo(customerId);
		example.or().andCustomerId2EqualTo(customerId);
		example.or().andCustomerId3EqualTo(customerId);
		return haMachineMapper.selectByExample(example);
	}

	@Override
	public void deleteByPrimaryKeys(List<String> machineIdList) throws Exception {
		HaMachineExample example = new HaMachineExample();
		HaMachineExample.Criteria criteria = example.createCriteria();
		criteria.andMachineIdIn(machineIdList);
		haMachineMapper.deleteByExample(example);
	}

	@Override
	public HaMachine findMachineByMachineNum(String machineNum) throws Exception {
		HaMachineExample example = new HaMachineExample();
		HaMachineExample.Criteria criteria = example.createCriteria();
		criteria.andMachineNumEqualTo(machineNum);
		List<HaMachine> machines = haMachineMapper.selectByExample(example);
		if(Assert.isValidCollection(machines)){
			return machines.get(0);
		}else{
			return null;
		}
	}

	@Override
	public QueryBean<HaMachine> findMachineByCustomer(HaMachineQueryBean machineQueryBean) throws Exception {
		HaMachineExample example = new HaMachineExample();
		
		if(Assert.isInvalidString(machineQueryBean.getMachineNum()) && Assert.isInvalidString(machineQueryBean.getMachineCname())){
			example.or().andCustomerIdEqualTo(machineQueryBean.getCustomerId());
			example.or().andCustomerId1EqualTo(machineQueryBean.getCustomerId());
			example.or().andCustomerId2EqualTo(machineQueryBean.getCustomerId());
			example.or().andCustomerId3EqualTo(machineQueryBean.getCustomerId());
		}else{
			if(Assert.isValidString(machineQueryBean.getMachineNum())){
				example.or().andMachineNumLike("%"+machineQueryBean.getMachineNum()+"%").andCustomerIdEqualTo(machineQueryBean.getCustomerId());
				example.or().andMachineNumLike("%"+machineQueryBean.getMachineNum()+"%").andCustomerId1EqualTo(machineQueryBean.getCustomerId());
				example.or().andMachineNumLike("%"+machineQueryBean.getMachineNum()+"%").andCustomerId2EqualTo(machineQueryBean.getCustomerId());
				example.or().andMachineNumLike("%"+machineQueryBean.getMachineNum()+"%").andCustomerId3EqualTo(machineQueryBean.getCustomerId());
			}
			if(Assert.isValidString(machineQueryBean.getMachineCname())){
				example.or().andMachineCnameLike("%"+machineQueryBean.getMachineCname()+"%").andCustomerIdEqualTo(machineQueryBean.getCustomerId());
				example.or().andMachineCnameLike("%"+machineQueryBean.getMachineCname()+"%").andCustomerId1EqualTo(machineQueryBean.getCustomerId());
				example.or().andMachineCnameLike("%"+machineQueryBean.getMachineCname()+"%").andCustomerId2EqualTo(machineQueryBean.getCustomerId());
				example.or().andMachineCnameLike("%"+machineQueryBean.getMachineCname()+"%").andCustomerId3EqualTo(machineQueryBean.getCustomerId());
			}
		}
		machineQueryBean.setItemCount(haMachineMapper.countByExample(example));//总记录数
		if(machineQueryBean.getItemCount()>0){//计算总页数
			machineQueryBean.setPageCount(machineQueryBean.getItemCount()/machineQueryBean.getPageSize()+
					(int)(machineQueryBean.getItemCount()%machineQueryBean.getPageSize()>0?1:0));
			if(machineQueryBean.getItemCount()>machineQueryBean.getOffset()){
				if(Assert.isValidString(machineQueryBean.getOrderBy())){//排序
					example.setOrderByClause(machineQueryBean.getOrderBy()+" "+machineQueryBean.getOrder());
				}
				//分页
				example.setOffset(machineQueryBean.getOffset());
				example.setLimit(machineQueryBean.getLimit());
				machineQueryBean.setResult(haMachineMapper.selectByExample(example));
			}
		}
		
		return machineQueryBean;
	}

	@Override
	public List<HaMachine> findOnlineMachine() throws Exception {
		HaMachineExample example = new HaMachineExample();
		example.createCriteria().andMachineOnlineStatusEqualTo(1);
		return haMachineMapper.selectByExample(example);
	}

	@Override
	public List<HaMachine> findOutlineMachine() throws Exception {
		HaMachineExample example = new HaMachineExample();
		example.createCriteria().andMachineOnlineStatusEqualTo(0);
		return haMachineMapper.selectByExample(example);
	}
}